package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.service.WeChatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序组件业务类
 *
 * @Author mr.zhang
 * Date 2025/5/23 16:40
 */
@Service
public class WeChatServiceImpl implements WeChatService {

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    private static final String GET_OPENID_BY_CODE = "https://api.weixin.qq.com/sns/jscode2session";

    private static final String GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";

    private static final String GET_PHONE_BY_TOKEN = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    /**
     * 根据临时凭证获取openId
     *
     * @param code
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type","authorization_code");
        paramMap.put("appid",appId);
        paramMap.put("secret",appSecret);
        paramMap.put("js_code",code);

        String resultJSON = HttpUtil.get(GET_OPENID_BY_CODE,paramMap);
        if(StringUtils.isBlank(resultJSON)){
            throw new BaseException("调用微信小程序登录接口失败");
        }

        JSONObject jsonObject = JSON.parseObject(resultJSON);

        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("微信小程序登录接口失败");
        }


        return jsonObject.getString("openid");
    }

    /**
     * 获取手机号访问凭证
     */
    public String getToken(){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type","client_credential");
        paramMap.put("appid",appId);
        paramMap.put("secret",appSecret);

        //使用hutool工具类
        String resultJSON = HttpUtil.get(GET_TOKEN,paramMap);
        if(StringUtils.isBlank(resultJSON)){
            throw new BaseException("调用获取手机号访问凭证的接口失败");
        }
        JSONObject jsonObject = JSON.parseObject(resultJSON);
        /*Integer errcode = jsonObject.getInteger("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("调用获取手机号访问凭证的接口失败");
        }*/
        return jsonObject.getString("access_token");
    }

    /**
     * 根据临时凭证获取手机号
     *
     * @param code
     */
    @Override
    public String getPhone(String code) {
        String accessToken = getToken();
        Map<Object, Object> resultMap = new HashMap<>();
        resultMap.put("code",code);

        String resultJSON = HttpUtil.post(GET_PHONE_BY_TOKEN + accessToken, JSON.toJSONString(resultMap));
        if(StringUtils.isBlank(resultJSON)){
            throw new BaseException("通过临时凭证获取手机号的接口失败1");
        }

        JSONObject jsonObject = JSON.parseObject(resultJSON);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("通过临时凭证获取手机号的接口访问失败");
        }

        String phoneInfo = jsonObject.getString("phone_info");
        JSONObject jsonObject1 = JSON.parseObject(phoneInfo);

        return jsonObject1.getString("phoneNumber");

    }
}
